package com.gitee.wsl.mathematics.vector.ext

import com.gitee.wsl.math.Math
import com.gitee.wsl.mathematics.complex.quaternion.Quat
import com.gitee.wsl.mathematics.vector.vec2.Vector2
import com.gitee.wsl.mathematics.vector.vec3.Vector3
import com.gitee.wsl.mathematics.vector.vec4.Vector4

/** The vector with minimum components of vectors [a] and [b]. */
fun <T:Number> Math.maxComponents(a: Vector2<T, *>, b: Vector2<T, *>) = a.run { create(max(a.x, b.x), max(a.y, b.y)) }
/** The vector with minimum components of vectors [a] and [b]. */
fun <T:Number> Math.maxComponents(a: Vector2<T, *>, b: Vector3<T, *>) = b.run { create(max(a.x, b.x), max(a.y, b.y), max(zero, b.z))}
/** The vector with minimum components of vectors [a] and [b]. */
fun <T:Number> Math.maxComponents(a: Vector3<T, *>, b: Vector2<T, *>) = a.run { create(max(a.x, b.x), max(a.y, b.y), max(zero, a.z)) }
/** The vector with minimum components of vectors [a] and [b]. */
fun <T:Number> Math.maxComponents(a: Vector3<T, *>, b: Vector3<T, *>) = a.run { create(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z))}
/** The vector with minimum components of vectors [a] and [b]. */
fun <T:Number> Math.maxComponents(a: Vector4<T, *>, b: Vector4<T, *>) = a.run { create(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w))}
/** The vector with minimum components of vectors [a] and [b]. */
fun <T:Number> Math.maxComponents(a: Vector4<T, *>, b: Vector3<T, *>) = a.run { create(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, zero))}
/** The vector with minimum components of vectors [a] and [b]. */
fun <T:Number> Math.maxComponents(a: Vector4<T, *>, b: Vector2<T, *>) = a.run { create(max(a.x, b.x), max(a.y, b.y), max(a.z, zero), max(a.w, zero))}
/** The vector with minimum components of vectors [a] and [b]. */
fun <T:Number> Math.maxComponents(a: Vector3<T, *>, b: Vector4<T, *>) = b.run { create(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(zero, b.w))}
/** The vector with minimum components of vectors [a] and [b]. */
fun <T:Number> Math.maxComponents(a: Vector2<T, *>, b: Vector4<T, *>) = b.run { create(max(a.x, b.x), max(a.y, b.y), max(zero, b.z), max(zero, b.w))}
/** The quaternion with minimum components of quaternions [a] and [b]. */
fun Math.maxComponents(a: Quat, b: Quat) = a.run { create( max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w))}

fun <T:Number> Math.maxComponents(p1: Vector2<T, *>, vararg p2: Vector2<T, *>) = p1.run { create(
    max(p1.x, p2.maxBy { it.x.toDouble() }.x),
    max(p1.y, p2.maxBy { it.y.toDouble() }.y)
)
} as Vector2<T, *>